package com.steria.workshop.android.yrapp.lib.saxparser;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.res.AssetManager;
import android.util.Log;

import com.steria.workshop.android.yrapp.lib.model.WeatherData;

public class YrSaxParser {
	
	private SAXParserFactory saxParserFactory;
	private YrSaxHandler yrSaxHandler;
	private static int previousAssetFilePosition = 0;

	public YrSaxParser() {
		saxParserFactory = SAXParserFactory.newInstance();
		yrSaxHandler = new YrSaxHandler();
	}
	
	/**
	 * Method to retrieve weather data from a specific yr.no url, with a artificial delay to simulate network lag
	 * @param url yr.no url to collect weather data from
	 * @param delayInSeconds number of seconds the method should wait, to simulate network lag
	 * @return weather data
	 */
	public WeatherData parse(URL url, int delayInSeconds) {
		 waitForIt(delayInSeconds);
		 return parse(url);
	}

	public WeatherData parse(URL url) {
		try {
			return parse(url.openStream());
		} catch (IOException e) {
			Log.e(YrSaxParser.class.getSimpleName(), e.getMessage());
		}
		
		return new WeatherData();
	}

	public WeatherData parse(String url) {
		try {
			return parse(new URL(url));
		} catch (MalformedURLException e) {
			Log.e(YrSaxParser.class.getSimpleName(), "Not a valid url string", e);
		}

		return new WeatherData();
	}
	
	public WeatherData parse(String url, int delayInSeconds) {
		waitForIt(delayInSeconds);
		return parse(url);
	}
	
	public WeatherData parse(AssetManager manager) {
		try {
			return parse(manager.open(getRandomAsset()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new WeatherData();
	}
	
	public WeatherData parse(AssetManager manager, int delayInSeconds) {
		waitForIt(delayInSeconds);
		return parse(manager);
		
	}
	
	public WeatherData parse(InputStream stream) {
		try {
			SAXParser saxParser = saxParserFactory.newSAXParser();
			saxParser.parse(new InputSource(stream), yrSaxHandler);
		} catch (ParserConfigurationException e) {
			Log.e(YrSaxParser.class.getSimpleName(), e.getMessage());
		} catch (SAXException e) {
			Log.e(YrSaxParser.class.getSimpleName(), e.getMessage());
		} catch (FileNotFoundException e) {
			Log.e(YrSaxParser.class.getSimpleName(), e.getMessage());
		} catch (IOException e) {
			Log.e(YrSaxParser.class.getSimpleName(), e.getMessage());
		}
		return yrSaxHandler.getParsedItems();
	}
	
	private void waitForIt(int delayInSeconds) {
		if(delayInSeconds > 0) {
			synchronized (this) {
				try {
					Log.i(YrSaxParser.class.getSimpleName(), "Waiting " + delayInSeconds + " seconds before continuing");
					wait(delayInSeconds*1000);
				} catch (InterruptedException e) {
					Log.e(YrSaxParser.class.getSimpleName(), "Error while waiting", e);
				}
			}
		}
	}
	
	private String getRandomAsset() {
		String[] assets = new String[] {"weatherdata1.xml", "weatherdata2.xml"};
		
		if(previousAssetFilePosition > 0) {
			previousAssetFilePosition = 0;
		} else {
			previousAssetFilePosition++;
		}
		
		return assets[previousAssetFilePosition];
	}

}
